package com.senlingspace.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.PageUtils;
import com.senlingspace.system.domain.dto.TeamStatusDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.senlingspace.system.mapper.SlkTeamStatusMapper;
import com.senlingspace.system.domain.SlkTeamStatus;
import com.senlingspace.system.service.ISlkTeamStatusService;

/**
 * 门店组队信息Service业务层处理
 *
 * @author SenlingSpace
 * @date 2024-08-19
 */
@Service
public class SlkTeamStatusServiceImpl implements ISlkTeamStatusService {
    private static final Logger log = LoggerFactory.getLogger(SlkTeamStatusServiceImpl.class);
    @Autowired
    private SlkTeamStatusMapper slkTeamStatusMapper;


    @Override
    public List<TeamStatusDto> listByIntend(Long shopId) {

        SlkTeamStatus slkTeamStatus = new SlkTeamStatus();
        slkTeamStatus.setShopId(shopId);
        slkTeamStatus.setTeamStatus("CREATE");
        List<TeamStatusDto> teamStatusDtos = selectSlkTeamStatusList(slkTeamStatus);
        return teamStatusDtos;
    }

    @Override
    public TeamStatusDto updateByStatusByInit(Long shopId) {
        /*查询可用的组队信息*/
        SlkTeamStatus slkTeamStatus = new SlkTeamStatus();
        slkTeamStatus.setShopId(shopId);
        slkTeamStatus.setTeamStatus("INIT");
        PageUtils.startPage(1, 1);
        List<SlkTeamStatus> slkTeamStatuses = selectList(slkTeamStatus);
        if (slkTeamStatuses == null || slkTeamStatuses.size() == 0) {
            log.error("门店id： " + shopId + "没有可用组队状态，导致新增可以状态时失败");

            return new TeamStatusDto();
        }
        /*更新一条可用组队信息为使用中*/
        slkTeamStatus = slkTeamStatuses.get(0);
        slkTeamStatus.setTeamStatus("CREATE");
        updateSlkTeamStatus(slkTeamStatus);

        TeamStatusDto teamStatusDto = new TeamStatusDto();
        BeanUtils.copyProperties(slkTeamStatus, teamStatusDto);


//        /*查询所有使用的中的组队信息返回*/
//        List<TeamStatusDto> teamStatusDtos = listByIntend(shopId);

        return teamStatusDto;
    }


    /**
     * 查询门店组队信息列表
     *
     * @param slkTeamStatus 门店组队信息
     * @return 门店组队信息
     */
    public List<SlkTeamStatus> selectList(SlkTeamStatus slkTeamStatus) {
        return slkTeamStatusMapper.selectList(slkTeamStatus);
    }


    /**
     * 查询门店组队信息列表
     *
     * @param slkTeamStatus 门店组队信息
     * @return 门店组队信息
     */
    @Override
    public List<TeamStatusDto> selectSlkTeamStatusList(SlkTeamStatus slkTeamStatus) {
        return slkTeamStatusMapper.selectSlkTeamStatusList(slkTeamStatus);
    }


    @Override
    public Boolean initByShopId(Long shopId, Long number) {

        SlkTeamStatus slkTeamStatus = new SlkTeamStatus();
        slkTeamStatus.setShopId(shopId);
        List<TeamStatusDto> teamStatusDtos = selectSlkTeamStatusList(slkTeamStatus);
        if (teamStatusDtos != null && teamStatusDtos.size() != 0) {
            log.warn("查询指定门店: " + shopId + " 已存在组队数据，无法初始化");
            return false;
        }

        List<SlkTeamStatus> slkTeamStatuses = generateTeams(shopId, number);

        int res = slkTeamStatusMapper.initByShopId(slkTeamStatuses);

        if (res == number) {
            return true;
        } else {
            return false;
        }
    }


    public List<SlkTeamStatus> generateTeams(Long shopId, Long count) {
        List<SlkTeamStatus> teamList = new ArrayList<>();
        int teamNameIndex = 0;
        for (int i = 0; i < count; i++) {
            SlkTeamStatus team = new SlkTeamStatus();
            team.setTeamName(generateTeamName(teamNameIndex));
            team.setShopId(shopId);
            team.setTeamStatus("INIT");
            teamList.add(team);
            teamNameIndex++;
        }
        return teamList;
    }

    // 根据索引生成组队名，从A-Z，然后是AA-AZ，依次递增
    public String generateTeamName(int index) {
        StringBuilder name = new StringBuilder();
        index++;  // 从1开始
        while (index > 0) {
            index--;  // 调整为0基数
            name.insert(0, (char) ('A' + index % 26));
            index /= 26;
        }
        return name.toString();
    }


    /**
     * 查询门店组队信息
     *
     * @param id 门店组队信息主键
     * @return 门店组队信息
     */
    @Override
    public SlkTeamStatus selectSlkTeamStatusById(Long id) {
        return slkTeamStatusMapper.selectSlkTeamStatusById(id);
    }


    /**
     * 新增门店组队信息
     *
     * @param slkTeamStatus 门店组队信息
     * @return 结果
     */
    @Override
    public int insertSlkTeamStatus(SlkTeamStatus slkTeamStatus) {
        slkTeamStatus.setCreateTime(DateUtils.getNowDate());
        return slkTeamStatusMapper.insertSlkTeamStatus(slkTeamStatus);
    }

    /**
     * 修改门店组队信息
     *
     * @param slkTeamStatus 门店组队信息
     * @return 结果
     */
    @Override
    public int updateSlkTeamStatus(SlkTeamStatus slkTeamStatus) {
        slkTeamStatus.setUpdateTime(DateUtils.getNowDate());
        return slkTeamStatusMapper.updateSlkTeamStatus(slkTeamStatus);
    }

    /**
     * 批量删除门店组队信息
     *
     * @param teamIds 需要删除的门店组队信息主键
     * @return 结果
     */
    @Override
    public int deleteSlkTeamStatusByIds(Long[] teamIds) {
        return slkTeamStatusMapper.deleteSlkTeamStatusByIds(teamIds);
    }

    /**
     * 删除门店组队信息信息
     *
     * @param teamId 门店组队信息主键
     * @return 结果
     */
    @Override
    public int deleteSlkTeamStatusById(Long teamId) {
        return slkTeamStatusMapper.deleteSlkTeamStatusById(teamId);
    }


}
